package com.hrm.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

public class MyJaxb2Marshaller extends Jaxb2Marshaller {
	@Override
	@SuppressWarnings("rawtypes")
	public boolean supports(Class clazz) {
		return super.supports(clazz) ? true : AnnotationUtils.findAnnotation(
				clazz, XmlRootElement.class) != null;
	}



	@Override
	public void setClassesToBeBound(Class<?>... classesToBeBound) {
		File[] files = {};
		List<String> list = new ArrayList<String>();
		String path;
		try {
			path = MyJaxb2Marshaller.class.getClassLoader().getResource("")
					.getPath();
			path += packageName.replaceAll("\\.", "/") + "/";
			File f = new File(path);
			if (f.isDirectory()) {
				files = f.listFiles();
			}
			for (int i = 0; i < files.length; i++) {
				String className = packageName
						+ "."
						+ files[i].getName().substring(0,
								files[i].getName().lastIndexOf("."));
				list.add(className);
			}
			Class<?>[] classes = new Class<?>[list.size()];
			for (int i = 0; i < list.size(); i++) {
				classes[i] = Class.forName(list.get(i));
			}
			classesToBeBound=classes;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		super.setClassesToBeBound(classesToBeBound);
	}

	private String packageName;

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

}